﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UnwiseSource.ServiceReference1;

namespace UnwiseSource.Administration.ToolClasses
{
    public static class RightsChecker
    {
        private static string MASTER = "Administrator";

        public static Rights GetRightsForCourse(AdminCourse course, UserCredibility user)
        {
            if (user.Role == MASTER)
            {
                return Rights.Admin;
            }

            if (course.CreatorId == user.UserId)
            {
                return Rights.Owner;
            }

            if (course.Contributors.Contains(user.UserId))
            {
                return Rights.Contributor;
            }

            return Rights.Guest;
        }

        public static Rights GetRightsForFullCourse(FullCourse course, UserCredibility user)
        {
            if (user.Role == MASTER)
            {
                return Rights.Admin;
            }

            foreach (var contributor in course.Contributors)
            {
                if (contributor.UserId == user.UserId)
                {
                    if (contributor.Contribution == "Owner")
                    {
                        return Rights.Owner;
                    }
                    else if (contributor.Contribution == "Contributor")
                    {
                        return Rights.Contributor;
                    }
                }
            }

            return Rights.Guest;
        }
    }
}